<?php

namespace app\model;

use think\Model;

class AdminMenu extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'admin_menu';
    // 设置当前模型主键
    protected $pk = 'id';

    // 获取所有数据
    public function getAllData($fields = 'id, pid, name, path, icon, hide, sort', $order = 'sort asc')
    {
        $data = $this::field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    // 根据条件获取数据
    public function getDataByWhere($where, $fields = 'id, pid, name, path, icon, hide', $order = 'sort asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 新增/更新
     * 
     * @dateTime 2022/09/13
     * @param array $params
     * @return fixed
     */
    public function edit($params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    // 根据pid获取数据
    public function getInfoByPid($pid, $fields = 'id')
    {
        $data = $this::where(['pid' => $pid])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    // 根据id获取数据
    public function getInfoById($id, $fields = 'id')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }
}